<?php
$this->LoadHelper('AttachHelper');
$tb = empty($this->Param['tb']) ? 'order_inquirycert' : $this->Param['tb'];
$AttachHelper = new AttachHelper($this->UserConfig['Id'], $tb);
$this->LoadHelper('MemberHelper');
$MemberHelper = new MemberHelper();
$member_info = $MemberHelper->getMember($this->UserConfig['Id']);
$this->Assign('member_info', $member_info);
$picture_array = array('order_inquirycert','order_family','order_sign','getcar_img','estimate_car');
switch ($this->Param['option']){
    case 'upattach':
        $type = empty($this->Param['type']) ? 'default' : $this->Param['type'];
        $filebox = empty($this->Param['filebox']) ? 'file' : $this->Param['filebox'];
        /**
         * 2018-3-14修改为base64上传
         */
        $file = $this->Param[$filebox];
        $do = $AttachHelper->base64Upload($file);
        if($do['errcode'] == 0){
            ErrorMsg::Json($do['data'], TRUE);
        }else{
            ErrorMsg::Json($do['msg']);
        }
        break;
    case 'upimage':
        $type = empty($this->Param['type']) ? 'default' : $this->Param['type'];
        $filebox = empty($this->Param['filebox']) ? 'file' : $this->Param['filebox'];
        /**
         * 2018-3-14修改为base64上传
         */
        $file = $this->Param[$filebox];
        $do = $AttachHelper->base64Upload($file);
        if($do['errcode'] == 0){
            $this->LoadResurces('aliyun/class.idcard');
            $idcardOcr = new Idcard();
            if($type == 'face'){
                $card_info = $idcardOcr->getCardInfo(trim($do['data']['save_path'],'/'), 'face');
            }elseif($type == 'back'){
                $card_info = $idcardOcr->getCardInfo(trim($do['data']['save_path'],'/'), 'back');
            }
            $do['data']['cardinfo'] = $card_info['data'];
            if(!in_array($tb,$picture_array)){
                //文字识别OCR
                $this->LoadResurces('baidu/AipOcr');
                $client = new AipOcr('10926083', '2NWzy0KW30qBjy1eMvNv7IFF', 'eAei4XVOmYwbkAIFkHfopMqgpKIZy9F6');
                $text = $client->basicAccurate(file_get_contents(trim($do['data']['save_path'],'/')));
                //$do['text'] = $text;
                //插入附件表
                $str = json_encode($text);
                $str = preg_replace_callback("#\\\u([0-9a-f]{4})#i",function($matchs){
                    return iconv('UCS-2BE', 'UTF-8', pack('H4', $matchs[1]));
                },$str);
            }
            $attach = array(
                'ot_oid' => !empty($this->Param['oid']) ? $this->Param['oid'] : 0,
                'temp_id' => !empty($this->Param['temp_id']) ? $this->Param['temp_id'] : 0,
                'type' => $do['data']['type'],
                'class' => $tb,
                'url' => $do['data']['save_path'],
                'img_text' => $str,
                'is_ftp' => $do['data']['up_ftp'],
                'user_name' => $member_info['u_realname'],
                'image_name' => $this->Param['ofilename']
            );
            $at_id = $AttachHelper->attachSave($attach);            
            ErrorMsg::Json($do['data'], TRUE);
        }else{
            ErrorMsg::Json($do['msg']);
        }
        die();
        /**=====================================================================================================**/
        $file = $_FILES[$filebox];
        $do = $AttachHelper->upImage($file);
        if($do['path']){
            $this->LoadResurces('aliyun/class.idcard');
            $idcardOcr = new Idcard();
            if($type == 'face'){
                $card_info = $idcardOcr->getCardInfo(trim($do['path'],'/'), 'face');
            }elseif($type == 'back'){
                $card_info = $idcardOcr->getCardInfo(trim($do['path'],'/'), 'back');
            }
            $do['cardinfo'] = $card_info['data'];
//            if(in_array($type, array('face'))){
//                if($card_info['success']){
//                    ErrorMsg::Json($do, TRUE);
//                }else{
//                    ErrorMsg::Json($card_info['msg']);
//                }
//            }else{
                ErrorMsg::Json($do, TRUE);
//            }

        }else{
            ErrorMsg::Json('处理失败');
        }
        
        break;
    case 'video':
        set_time_limit(0);        
        $type = empty($this->Param['type']) ? 'order_video' : $this->Param['type'];
        $filebox = empty($this->Param['filebox']) ? 'file' : $this->Param['filebox'];
        $file = $_FILES[$filebox];
        //漏洞
        $ext = array('mp4', 'wav', 'wma', 'wmv', 'flv', 'avi', 'mpg', 'asf', 'rm', 'rmvb');
        $temp_arr = explode(".", $file['name']);
    $file_ext = array_pop($temp_arr);
    $file_ext = trim($file_ext);
    $file_ext = strtolower($file_ext);
        if(!in_array($file_ext, $ext)){
            ErrorMsg::Json('请上传视频文件');
        }
        $do = $AttachHelper->upFile($file);
        if($do['path']){
            ErrorMsg::Json($do, TRUE);
        }else{
            ErrorMsg::Json('处理失败');
        }
        break;
}